package com.core.controller.User;

import com.core.pojo.Result.PageResult;
import com.core.pojo.Result.Result;
import com.core.pojo.SeatReservation.entity.Reservation;
import com.core.pojo.User.entity.User;
import com.core.pojo.User.entity.UserContext;
import com.core.service.User.UserInfoService;
import com.core.service.User.UserLoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/user")
@Tag(name = "用户基础信息模块")
@RequiredArgsConstructor
public class UserInfoController {
    private final UserLoginService userLoginService;
    private final UserInfoService userInfoService;

    @Operation(summary = "根据jwt token里的学号（id）查询用户信息")
    @GetMapping("/query")
    public Result<User> queryUserInfo() {
        Long userId= UserContext.getUserId();
        return Result.success(userLoginService.queryUserById(userId));
    }

    @Operation(summary = "修改用户信息")
    @PostMapping("/update")
    public Result<User> updateUserInfo(@RequestBody User user) {
        Long userId= UserContext.getUserId();
        if(!Objects.equals(userId, user.getId())){
            return Result.error("不能修改其它学生的信息");
        }
        return userInfoService.updateUserInfo(user);
    }

    @Operation(summary = "获取该用户预约记录（分页）")
    @GetMapping("/reservationHistory")
    public Result<PageResult<Reservation>> queryLocalUserReservations(@RequestParam(defaultValue = "1") int page,
                                                                      @RequestParam(defaultValue = "10") int pageSize) {
        return userInfoService.queryUserReservations(page,pageSize);
    }

}
